<?php
	$layout = $this->cmsSearch('', '100%'); 
	echo $layout;
	$layout = $this->cmsSubmenu(); 
	echo $layout;
	
	$items = $this->items;
	$loai_tin = $this->arrParam['loaitin'];
	$itemCountPerPage = $this->panigator->getItemCountPerPage();
	$frm_sendmail = $this->form_sendmailtoposter;
?>

<div id="wrapcontent">
	<div id="sidebar1">
		<?php
			$img = '<img src="' . $this->baseUrl(PUBLIC_PATH . "/img/icon/icon_hot.gif") . '" />';
			$block = $this->cmsBlock('Phòng trọ', $img, $this->itemsHotPtrTinTro); 
			echo $block;
		?>
		<!-- Begin Block hỗ trợ trực tuyến -->
		<?php 
			$block = $this->cmsBlock('Hỗ trợ trực tuyến', '', $this->thong_tin_cty, 3); 
			echo $block;
		?>
		<!-- End Block hỗ trợ trực tuyến -->
		<!-- Begin Block thống kê truy cập -->
		<?php 
			$block = $this->cmsBlock('Thống kê truy cập', '', 'info', 4); 
			echo $block;
		?>
		<!-- End Block thống kê truy cập -->
		<br class="clear" />
	</div>
	<div id="sidebar2">
		<div class="block">
			<div class="title">
				<span>Mạng xã hội</span>
			</div>
			<div class="content">
				<div class="follow">
					<div class="addthis_toolbox addthis_32x32_style addthis_vertical_style">
						<a class="addthis_button_facebook_follow" addthis:userid="kenhnhatro.vn"></a>
						<a class="addthis_button_twitter_follow" addthis:userid="kenh_nhatro"></a>
					</div>
				</div>
			</div>
			<br class="clear" />
		</div>
		<?php
			$img = '<img src="' . $this->baseUrl(PUBLIC_PATH . "/img/icon/icon_new.gif") . '" />';
			$block = $this->cmsBlock('Tin rao mới', $img, $this->itemsNewPtrTinTro); 
			echo $block;
			$img = '<img class="icon" src="' . $this->baseUrl(PUBLIC_PATH . "/img/icon/highlights.png") . '" />';
			$block = $this->cmsBlock('Tin tức nổi bật', $img, $this->itemsHotTinTuc, 2);
			echo $block;
		?>
	</div>
	<div id="primary">
		<div class="block">
			<div class="title">
				<div class="icon-arrow"></div>
				<span><?php echo $this->title; ?></span>
			</div>
			<div class="content">
				<?php
					$html = '';
					// Dùng kiểm tra hiển thị khung cho tin VIP
					$checkVipFirst = true;
					$checkVipEnd = false;
					$count = 0;
					foreach ($items as $val) {
						$id_tin_rao	= $val->id;
						$tieu_de 	= $val->tieu_de;
						$hinh_anh	= '/phong_tro/avatar/' . $val->hinh_anh;
						$luot_xem 	= $val->luot_xem;
						$ngay_dang 	= $val->ngay_dang;
						$dia_chi 	= $val->dia_chi;
						$goi_tin	= $val->goi_tin;
						$count++;
						$displayVIP = '';
						if ($goi_tin != 4) {
							$displayVIP = '<div class="icon_vip"></div>';
							if ($checkVipFirst) {
								$html .= '<div class="box-vip">';
								$checkVipFirst = false;
								$checkVipEnd = true;
							}
						} elseif ($checkVipEnd) {
							$html .= '</div>';
							$checkVipEnd = false;
						}
						
						$gia_phong 	= number_format($val->gia_o_ghep, 3, ",", ",") . ' VNĐ';
						if ($val->gia_o_ghep == null || $val->gia_o_ghep == 0)
							$gia_phong = 'Thỏa thuận';
						$dien_tich 	= $val->dien_tich_phong;
						$quan_huyen	= $val->getQuanHuyenObj();
						$nguoi_dang = $val->getNguoiDangObj();
						$email 		= $nguoi_dang->email;
						$html .= '<div class="item">';
						$html .= '<div class="metadata">';
						$html .= '<img src="' . $this->baseUrl(PUBLIC_IMG_URL . $hinh_anh) . '" alt="' . $val->tieu_de . '" width="64px" height="64px" onerror="this.onerror=null; this.src=\'' . $this->baseUrl(PUBLIC_IMG_URL . '/phong_tro/avatar/error_avatar_home.png') . '\';" />';
						$html .= '<ul>';
						$html .= '<li class="label-view">Số lượt xem</li>';
						$html .= '<li class="num-view">' . $luot_xem . '</li>';
						$html .= '</ul>';
						$html .= '</div>';
						$html .= '<a title="' . $tieu_de . '" rel="bookmark" href="' . $this->baseUrl("/oghtinrao/view/id/" . $id_tin_rao) . '">' . Zendhlt_Encode::truncateString_($tieu_de, 48). '</a>';
						$html .= '<div class="date post-date">' . $ngay_dang . $displayVIP . '</div>';
						$html .= '<div class="info">';
						$html .= 'Địa chỉ: ' . $dia_chi . '<br />';
						$html .= 'Giá phòng: <span class="price">' . $gia_phong . '</span><br />';
						$html .= 'Diện tích: ' . $dien_tich . ' m<sup>2</sup><br />';
						$html .= '<div class="district">' . $quan_huyen->ten . '<br />';
						$html .= '<a class="send_email" title="Gửi email đến người đăng" href="#sendMailModal" data-toggle="modal" onclick="setVal(\'oghtinrao\', ' . $id_tin_rao . ', ' . $nguoi_dang->id . ', \'' . $nguoi_dang->email . '\')"><img src="' . $this->baseUrl(DEFAULT_PATH . '/img/icon_email.png') . '" alt="' . $email . '" /></a>';
						$html .= '</div>';
						$html .= '</div>';
						$html .= '<div class="clear"></div></div>';
					}
					if ($checkVipEnd) {
						$html .= '</div>';
						$checkVipEnd = false;
					}
					if ($count == $itemCountPerPage && $checkVipEnd)
						$html .= '</div>';
					echo $html;
				?>
				<?php if ($this->totalItem > $itemCountPerPage) {?>
					<div class="pagenavi">
						<div class="pagination">
						<?php
							$currentUrl = $this->baseUrl($this->currentController . '/index/loaitin/' . $loai_tin);
							echo $this->paginationControl($this->panigator, 'Sliding', 'pagination.phtml', array('currentUrl' => $currentUrl));
						?>
						</div>
						<br class="clear" />
					</div>
				<?php } ?>
			</div>
			<br class="clear" />
		</div>
	</div>
	<!-- SEND MAIL CHO NGUOI DANG MODAL -->
	<div id="sendMailModal" class="modal hide fade" tabindex="-1"
		role="dialog" aria-labelledby="sendMailModalLabel" aria-hidden="true">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal"
				aria-hidden="true">×</button>
			<h3 id="sendMailModalLabel">Gửi cho người đăng tin</h3>
		</div>
		<div class="modal-body">
			<?php if ($this->state_login) {?>
			<div id="sendmailalert" class="alert"></div>
			<form class="form-horizontal">
				<?php 
					echo $frm_sendmail->loai_bai_dang;
					echo $frm_sendmail->id_bai_dang;
					echo $frm_sendmail->id_nguoi_dang;
					echo $frm_sendmail->email_nguoi_dang;
				?>
				<div class="control-group">
					<label class="control-label" for="nguoi_gui">Tên của bạn</label>
					<div class="controls">
					<?php echo $frm_sendmail->nguoi_gui;?>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="email_nguoi_gui">Email của bạn</label>
					<div class="controls">
					<?php echo $frm_sendmail->email_nguoi_gui;?>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="subject">Tiêu đề (Subject)</label>
					<div class="controls">
					<?php echo $frm_sendmail->subject;?>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="message">Thông điệp (Message)</label>
					<div class="controls">
						<?php echo $frm_sendmail->message;?>
					</div>
				</div>
			</form>
			<?php } else {?>
			<p class="errors">Phải đăng nhập mới có thể thực hiện thao tác này.</p>
			<?php } ?>
		</div>
		<div class="modal-footer">
			<button class="btn" data-dismiss="modal" aria-hidden="true">Bỏ qua</button>
			<?php if ($this->state_login) {?>
			<button id="btnSendMail" class="btn btn-primary">Gửi</button>
			<?php } else { ?>
			<a class="btn btn-primary" onclick="loginbtnClick()" data-dismiss="modal">Đăng nhập</a>
			<?php } ?>
		</div>
	</div>
	<script type="text/javascript">
		//Catch keydown event of the enter key
		$('#sendMailModal').keydown(function (e) {
			if(e.keyCode == 13) {
				sendForFriendProcess(e);
			}
		});
		//Catch click event of the send mail button
		$('#btnSendMail').click(function(e) {
			sendMailProcess(e);
		});
		
		$('#sendmail').click(function() {
			$('#sendmailalert').hide();
		});
		
		//Show the modal event processing
		$('#sendMailModal').on('show', function () {
			$(this).find('.alert').hide();
			$(this).find("input").each(function() {
				var inputid = $(this).attr('id');
				$("#"+inputid).parent().find('.errors').remove();
				$("#"+inputid+'[type="text"]').val("");
		    });
			$(this).find("textarea").each(function() {
				var inputid = $(this).attr('id');
				$("#"+inputid).val("");
		    });
		});
		
		$(function(){
			$('#sendMailModal').find("input").each(function() {
		        var inputid = $(this).attr('id');
		        $("#"+inputid).blur(function() {
		        	if(doValidation('sendMailModal', inputid, '/ajax/validatesendmail', null))
		        		$("#btnSendMail").removeAttr("disabled");
		        	else
		        		$("#btnSendMail").attr("disabled", "disabled");	
		        });
		    });
		});
		//Process send mail function
		function sendMailProcess(event) {
			var url = '/ajax/sendmail';
			var $this = $(this);
			var postData = $("#sendMailModal form").serializeObject();
			postData.format = "json";
			var isValid = true;
			$('#sendMailModal').find("input").each(function() {
				var inputid = $(this).attr('id');
				isValid = isValid&&doValidation('sendMailModal', inputid, '/ajax/validatesendmail', 'validate');
		    });
			if(isValid){
				$("#btnSendMail").attr("disabled", "disabled");
				$.ajax({
		            type: 'POST',
		            url: url,
		            async: true,
		            cache: true,
		            data: postData,
		            dataType: postData.format,
		            success: function(data){
		            	if(data.result) {
	                		$('#sendmailalert').html(data.message);
	                		$('#sendmailalert').show();
		            		setTimeout(function() {
								$('#sendMailModal').modal('hide'); 
							}, 1000);
		            	} else {
		            		if(data.message){
		                		$('#sendmailalert').html(data.message);
		                		$('#sendmailalert').show();
		                    }
		                }
		            }
		        });
				$("#btnSendMail").removeAttr("disabled");
			}
		}

		function setVal (loai, id, ndang, endang) {
			$('#loai_bai_dang').val(loai);
			$('#id_bai_dang').val(id);
			$('#id_nguoi_dang').val(ndang);
			$('#email_nguoi_dang').val(endang);
		}
	</script>
   	<div class="clear"></div>
</div>